/*
 *	Copyright 2011 andro-inject developers 
 *	Licensed under the Apache License, Version 2.0 (the "License"); 
 *	you may not use this file except in compliance with the License. 
 *	You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 *	Unless required by applicable law or agreed to in writing, software 
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License. 
 * 
*/

package com.androinject.test.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.androinject.core.definitions.ComponentDefinition;
import com.androinject.core.definitions.ComponentDefinitions;
import com.androinject.core.definitions.ComponentProperty;
import com.androinject.core.definitions.ManagedComponentDefinition;

public class Utilities {

	public static void parseXmlFile(DefaultHandler handler, String file) {
		try {
			SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
			XMLReader xmlReader = saxParser.getXMLReader();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(new InputSource(file));
		} catch (Exception e) {
			throw new TestException(e);
		}
	}
	
	
	public static void checkManagedComponentDefinition(ComponentDefinitions definitions, String id, String initMethod) {
		ManagedComponentDefinition definition = definitions.getManagedComponentDefinition(id);
		assertNotNull(definition);
		assertEquals(definition.getId(), id);
		assertEquals(initMethod, definition.getInitMethod());
	}
	
	
	public static void checkComponentDefinition(ComponentDefinitions definitions, String id, String className, boolean lazyInit, String initMethod) {
		ComponentDefinition definition = definitions.getComponentDefinition(id);
		assertNotNull(definition);
		assertEquals(definition.getId(), id);
		assertEquals(definition.getClassName(), className);
		assertEquals(initMethod, definition.getInitMethod());
		assertEquals(lazyInit, definition.usesLazyInitialization());
	}
	
	
	public static ComponentProperty createPropertyWithReference(String name, String reference) {
		ComponentProperty property = new ComponentProperty();
		property.setName(name);
		property.setReferencedComponentId(reference);
		return property;
	}
	
	
	public static void checkProperties(List<ComponentProperty> toCompare, ComponentProperty... expected) {
		assertEquals(toCompare.size(), expected.length);
		for (ComponentProperty expectedProperty : expected) {
			
			boolean found = false;
			for (int i = 0; i < toCompare.size() && !found; i++) {
				ComponentProperty property = toCompare.get(i);
				if (property.getName().equals(expectedProperty.getName())) {
					assertEquals(property.getReferencedComponentId(), expectedProperty.getReferencedComponentId());
					found = true;
				}
			}

			assertTrue(found);
		}
	}
	
	
	
	public static class TestException extends RuntimeException {
		
		private static final long serialVersionUID = 1L;
		
		public TestException() { }
		
		public TestException(Exception cause) {
			super(cause);
		}
	}
}
